<?php

namespace app\index\controller\source;

use app\index\model\UM as UM;
use app\index\service\SourceOrderService;
use ShidianUtil\Thinkphp\Response\ApiResponse;
use ShidianUtil\Utils\CheckUtil;
use think\APP;
use think\Request as Request;

/**
 * SourceOrder 控制器
 * Class SourceOrder
 * @package app\index\controller\source;
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037509
 */
class SourceOrder
{
    // 请求参数
    private Request $request;

    // service
    private SourceOrderService $service;

    public function __construct(Request $request, SourceOrderService $sourceOrderService)
    {
        $this->request = $request;
        $this->service = $sourceOrderService;
    }

    /**
     * 列表
     * @param UM|null $user
     * @return ApiResponse
     * @url /source/source_order/index
     */
    public function index(UM $user): ApiResponse
    {
        $request = $this->request;

        $sourceStatus = $request->param('sourceStatus', null);
        $sourceType = $request->param('sourceType', null);
        $page = $request->param('page', 1);
        $page_size = $request->param('page_size', 20);

        return ApiResponse::success($this->service->index($user, $sourceStatus, $sourceType, $page, $page_size));
    }

    /**
     * 详情
     * @param UM|null $user
     * @return ApiResponse
     * @url /source/source_order/read
     */
    public function read(?UM $user): ApiResponse
    {
        $request = $this->request;

        $id = $request->param('id');

        if (!CheckUtil::isId($id)) {
            return ApiResponse::fail('缺少id参数');
        }

        return ApiResponse::success($this->service->read($user, $id));
    }

    public function save(UM $user): ApiResponse
    {
        $request = $this->request;

        $id = $request->param('id');

        if (!CheckUtil::isId($id)) {
            return ApiResponse::fail('缺少id参数');
        }

        $this->service->modify($user, $request->param('id'), $request->param());

        return ApiResponse::success(null);
    }

}
